package com.example.pattern.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import java.lang.ref.WeakReference

class SPUtil private constructor(){
    private lateinit var weakContext:WeakReference<Context>
    private val sp:SharedPreferences by lazy {
       weakContext.get()!!.getSharedPreferences(Constants.SHARED_FILE_NAME,Context.MODE_PRIVATE)
    }
    //单例对象
    companion object{
       private var instance:SPUtil? = null
        fun defaultUtils(context:Context):SPUtil{
            return instance?:SPUtil().also{
                instance = it
                it.weakContext = WeakReference(context)
            }
        }
    }
    var isFirst:Boolean = true

        set(value){
            field = value
            sp.edit().also {
                it.putBoolean(Constants.IsFirstKey,value)
                it.apply()
            }
        }
        get(){
            return sp.getBoolean(Constants.IsFirstKey,true)
        }
}